<template>
  <div>
    <el-dialog :title="'选择'+pType" :visible.sync="dialogTableVisible">
      <div class="departSearch">
        <el-input v-model="input" placeholder="请输入内容" class="pop_input" />
        <el-button type="success" class="pop_btn" @click="searchFun">搜索</el-button>
        <el-button type="primary" class="pop_btn" @click="sureFun">确认</el-button>
      </div>
      <Graytable :t-head="tHead" :t-data="tData" @selectionChange="selectionChange" />
      <pagination
        :total="query.total"
        :page.sync="query.page"
        :limit.sync="query.limit"
        @pagination="getList"
      />
    </el-dialog>
  </div>
</template>

<script>
import {} from "@/api/rad";
export default {
  name: "Poptable",
  props: {
    pType: {
      required: true,
      type: String
    },
    ctype: {
      type: String,
      default: "mz"
    },
    setting: {
      type: Boolean,
      default: false
    },
    Patient_id: {
      type: String,
      default: ""
    },
    Visit_id: {
      type: String,
      default: ""
    }
  },
  data() {
    return {
      dialogTableVisible: false,
      query: {
        total: 0,
        page: 1,
        limit: 10
      },
      tHead: [],
      tData: [],
      popFlag: true,
      input: "",
      srm: "",
      type: "",
      selectLists: "",
      title: ""
    };
  },
  watch: {
    pType(e) {
      this.type = this.pType;
      this.title = "选择" + this.type;
      this.changeFun();
    },
    ctype() {
      this.changeFun();
    },
    Patient_id() {
      this.changeFun();
    },
    Visit_id() {
      this.changeFun();
    }
  },
  methods: {
    changeFun() {
      this.query.page = 1;
      this.query.limit = 10;
      this.srm = "";
      this.input = "";
      this.query.total = 0;
      this.tData = [];
      if (this.type === "科室") {
        this.tHead = [
          { selection: true },
          { prop: "deptcode", label: "科室编码" },
          { prop: "deptname", label: "科室名称" }
        ];
      } else if (this.type === "主管医生") {
        this.tHead = [
          { selection: true },
          { prop: "doctorcode", label: "主管医生编码" },
          { prop: "doctorname", label: "主管医生姓名" }
        ];
      } else if (this.type === "药疗医嘱" || this.type === "咨询对象") {
        this.tHead = [
          { selection: true },
          { prop: "uniquecode", label: "药疗医嘱编码" },
          { prop: "drugname", label: "药疗医嘱名称" }
        ];
      } else if (this.type === "给药途径") {
        this.tHead = [
          { selection: true },
          { prop: "routecode", label: "给药途径编码" },
          { prop: "routename", label: "给药途径名称" }
        ];
      } else if (this.type === "诊断") {
        this.tHead = [
          { selection: true },
          { prop: "discode", label: "诊断编码" },
          { prop: "disname", label: "诊断名称" }
        ];
      } else if (this.type === "药品") {
        this.tHead = [
          { selection: true },
          { prop: "uniquecode", label: "药品编码" },
          { prop: "drugname", label: "药品名称" }
        ];
      } else if (this.type === "问题类型") {
        this.tHead = [
          { selection: true },
          { prop: "commentsname", label: "问题类型" }
        ];
      } else if (
        this.type === "导入医嘱对象" ||
        this.type === "导入医嘱要点" ||
        this.type === "用药建议医嘱对象" ||
        this.type === "用药建议医嘱要点" ||
        this.type === "简要记录-医嘱对象" ||
        this.type === "简要记录-医嘱要点"
      ) {
        this.tHead = [
          { selection: true },
          { prop: "ordername", label: "药品名称" },
          { prop: "usagedosage", label: "用法用量" },
          { prop: "startdatetime", label: "开嘱时间" }
        ];
      } else if (
        this.type === "导入检验" ||
        this.type === "用药建议导入检验" ||
        this.type === "简要记录-导入检验"
      ) {
        this.tHead = [
          { selection: true },
          { prop: "labname", label: "检验名称" },
          { prop: "reporttime", label: "检验报告时间" },
          { prop: "labresult", label: "检验结果" }
        ];
      }
      this.getList();
    },
    async getList() {
      let res;
      if (this.type === "科室") {
        res = await gslGetDeptListJson({
          pagesize: this.query.limit,
          pagenum: this.query.page,
          keyword: this.srm
        });
        this.tData = res.rows;
        this.query.total = res.records;
      } else if (this.type === "主管医生") {
        res = await gslGetDictDoctorListJson({
          pagesize: this.query.limit,
          pagenum: this.query.page,
          keyword: this.srm
        });
        this.tData = res.Doctordata;
        this.query.total = res.records;
      } else if (
        this.type === "药疗医嘱" ||
        this.type === "药品" ||
        this.type === "咨询对象"
      ) {
        res = await gslGetDrugList({
          pagesize: this.query.limit,
          pagenum: this.query.page,
          keyword: this.srm
        });
        this.tData = res.rows;
        this.query.total = res.records;
      } else if (this.type === "给药途径") {
        res = await gslGetDictRouteListJson({
          pagesize: this.query.limit,
          pagenum: this.query.page,
          keyword: this.srm
        });
        this.tData = res.Doctordata;
        this.query.total = res.records;
      } else if (this.type === "诊断") {
        res = await gslGetDictDiseaseListJson({
          pagesize: this.query.limit,
          pagenum: this.query.page,
          keyword: this.srm
        });
        this.tData = res.Dictdiseasedata;
        this.query.total = res.records;
      } else if (this.type === "问题类型") {
        res = await GetCommentListJson({
          pagesize: this.query.limit,
          pagenum: this.query.page,
          keyword: this.srm
        });
        this.tData = res.QuestionList;
        this.query.total = res.records;
      } else if (
        this.type === "导入医嘱对象" ||
        this.type === "导入医嘱要点" ||
        this.type === "用药建议医嘱对象" ||
        this.type === "用药建议医嘱要点" ||
        this.type === "简要记录-医嘱对象" ||
        this.type === "简要记录-医嘱要点"
      ) {
        res = await gslGetCurrentPatientOrdersList({
          pagesize: this.query.limit,
          pagenum: this.query.page,
          json: { Patient_id: this.Patient_id, Visit_id: this.Visit_id }
        });
        this.tData = res.CurrentPatientOrdersdata;
        console.log(res);
        this.query.total = res.CurrentPatientOrdersdata.length;
      } else if (
        this.type === "导入检验" ||
        this.type === "用药建议导入检验" ||
        this.type === "简要记录-导入检验"
      ) {
        res = await gslGetCurrentPatientLabList({
          pagesize: this.query.limit,
          pagenum: this.query.page,
          json: { Patient_id: this.Patient_id, Visit_id: this.Visit_id }
        });
        this.tData = res.CurrentPatientLabdata;
        this.query.total = res.CurrentPatientLabdata.length;
      }
    },
    show() {
      this.dialogTableVisible = true;
    },
    hide() {
      this.dialogTableVisible = false;
    },
    searchFun() {
      this.query = {
        page: 1,
        limit: 10,
        total: 0
      };
      this.srm = this.input;
      this.getList();
    },
    selectionChange(data) {
      this.selectLists = data;
    },
    sureFun() {
      if (this.type === "科室") {
        this.$emit("handleDeptcode", this.selectLists);
      }
      if (this.type === "主管医生") {
        this.$emit("handleDoctorcode", this.selectLists);
      }

      this.hide();
    }
  }
};
</script>
<style lang="scss" scoped="scoped">
.departSearch {
  display: flex;
  .pop_input {
    width: 300px;
  }
  .pop_btn {
    width: 70px;
    height: 40px;
    margin-left: 10px;
  }
}
</style>
